package cn.xiaobai.utils;

import cn.hutool.core.io.FileUtil;
import cn.xiaobai.domain.WhiteInventory;
import cn.xiaobai.enums.FirewalldTemplateEnum;
import cn.xioabai.common.enums.OsTypeEnum;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 处理脚本的生成
 *
 * @Author yangdaji
 * @Date 2023/2/27 0027
 * @PackageName cn.xiaobai.utils
 * @ClassName ScriptUtils
 */
public class ScriptUtils {

    /**
     * 生成并保存脚本文件,根据系统类型
     *
     * @param whiteInventory
     */
    public static void generateScriptByOsType(WhiteInventory whiteInventory) {
        OsTypeEnum osType = whiteInventory.getOsType();
        String saveFilePath = whiteInventory.getFilePath().replaceAll("\\\\", "/") + "/firewall-ip";
        Set<String> ipList = whiteInventory.getIpList();
        Set<String> portList = whiteInventory.getPortList();
        // 根据系统类型,保存的路径有所变化
        List<String> scriptList = new ArrayList<>();

        switch (osType) {
            case WINDOWS: {
                String ipPortWindowTemplate = FirewalldTemplateEnum.OP_IP_PORT_WINDOWS.getTemplate();
                scriptList.add("@echo off");
                //  加入防止执行脚本时中文乱码
                scriptList.add("chcp 65001");
                generateScript(ipList, portList, scriptList, ipPortWindowTemplate, osType);
                saveFilePath = saveFilePath + ".bat";
                break;
            }
            case LINUX: {
                String ipPortLinuxTemplate = FirewalldTemplateEnum.OP_IP_PORT_LINUX.getTemplate();
                scriptList.add("#!/bin/bash");
                generateScript(ipList, portList, scriptList, ipPortLinuxTemplate, osType);
                saveFilePath = saveFilePath + ".sh";
                break;
            }
            default:
                // todo 未适配该系统的防火墙脚本
                break;
        }
        File file = Paths.get(saveFilePath).toFile();
        if (file.exists()) {
            //删除原文件
            file.delete();
        }
        FileUtil.appendLines(scriptList, file, "UTF-8");
    }

    /**
     * 根据模版生成指定的脚步
     *
     * @param ipList     ip 集合
     * @param portList   端口集合
     * @param scriptList 脚本保存集合
     * @param template   脚本模版
     */
    public static void generateScript(Set<String> ipList, Set<String> portList, List<String> scriptList, String template, OsTypeEnum osType) {
        if (CollectionUtils.isEmpty(ipList) || CollectionUtils.isEmpty(portList)) {
            return;
        }
        // windows 系统需要单独处理
        if (OsTypeEnum.WINDOWS.getOsName().equals(osType.getOsName())) {
            StringBuilder ip = new StringBuilder();
            ipList.forEach(item -> ip.append(item).append(","));
            StringBuilder port = new StringBuilder();
            portList.forEach(item -> port.append(item).append(","));
            scriptList.add(template.replace("IP_NAME", ip).replace("PORT_NAME", port)
                    .replaceAll("\r\n", ""));
        } else {
            ipList.forEach(ip -> {
                String ipName = template.replace("IP_NAME", ip);
                portList.forEach(port -> scriptList.add(ipName.replace("PORT_NAME", port)
                        .replaceAll("\r\n", "")));
            });
        }
    }

}
